package com.jackiechan.day16;

public class SYN_Runnable {
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        Thread t0 = new Thread(myRunnable, "赵");
        Thread t1 = new Thread(myRunnable, "钱");
        Thread t2 = new Thread(myRunnable, "孙");
        Thread t3 = new Thread(myRunnable, "李");
        t0.start();
        t1.start();
        t2.start();
        t3.start();
    }

    static class MyRunnable implements Runnable {
        int count = 10000;
//        double start = 0, end = 0;

        @Override
        public void run() {
//            start = System.nanoTime();
            while (true) {
                synchronized (this) {
                    if (count > 0) {
                        System.out.println(Thread.currentThread().getName() + " 为您服务: 卖出" + (10000 - count + 1) + "号票,余票" + (--count));
                    } else {
                        System.out.println(Thread.currentThread().getName() + " 为您服务: 已售罄");
                        break;
                    }
                }
            }
//            end = System.nanoTime();
//            System.out.println(Thread.currentThread().getName() + " 已工作时长:" + (end - start) / 1000000 + "毫秒");
        }
    }
}
